<template>
  <pro-layout
    :menus="menus"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :siderWidth="280"
    :theme="navTheme"
    v-bind="settings"
  >
    <multi-tab v-if="settings.multiTab"></multi-tab>
    <transition name="page-transition"></transition>
    <!-- 1.0.0+ 版本 pro-layout 提供 API，
      我们推荐使用这种方式进行 LOGO 和 title 自定义
    -->
    <template v-slot:menuHeaderRender>
      <div>
        <logo-svg />
        <h1>{{ title }}</h1>
      </div>
    </template>
    <!-- 1.0.0+ 版本 pro-layout 提供 API,
      增加 Header 左侧内容区自定义
    -->
    <!-- <template v-slot:headerContentRender>
      <div style="height: 100%;display: flex;align-items: center;">
        <a-breadcrumb :routes="breadcrumbRoutes">
          <template slot="separator">
            <a-icon type="right" style="font-size:12px;" />
          </template>
          <template slot="itemRender" slot-scope="{ route, params, routes }">
            <span v-if="routes.indexOf(route) === routes.length - 1">
              {{ route.breadcrumbName }}
            </span>
            <router-link
              v-else
              :to="{
                path: route.path || '/',
                params: params
              }">
              {{ route.breadcrumbName }}
            </router-link>
          </template>
        </a-breadcrumb>
      </div>
    </template> -->
    <template v-slot:Header>
      <right-content :top-menu="settings.layout === 'topmenu'" :is-mobile="isMobile" :theme="settings.theme" />
    </template>
    <!-- <setting-drawer v-if="isDev" :settings="settings" @change="handleSettingChange"></setting-drawer> -->
    <template v-slot:rightContentRender>
      <right-content :top-menu="settings.layout === 'topmenu'" :is-mobile="isMobile" :theme="settings.theme" />
    </template>
    <!-- custom footer / 自定义Footer -->
    <!-- <template v-slot:footerRender>
      <global-footer />
    </template> -->
    <transition enter-active-class="page-fade-in">
      <router-view />
    </transition>
  </pro-layout>
</template>

<script>
// import { updateTheme } from '@ant-design-vue/pro-layout'
// import { i18nRender } from '@/locales'
import { mapState, mapGetters } from 'vuex'
import { CONTENT_WIDTH_TYPE, SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'

import defaultSettings from '@/config/defaultSettings'
// import GlobalFooter from '@/components/GlobalFooter'
import RightContent from '@/components/GlobalHeader/RightContent'
import MultiTab from '@/components/MultiTab'
import LogoSvg from '../assets/logo.svg?inline'

export default {
  name: 'BasicLayout',
  components: {
    RightContent,
    // GlobalFooter,
    LogoSvg,
    MultiTab
  },
  provide () {
    return {
      handleCollapse: this.handleCollapse
    }
  },
  data () {
    return {
      // preview.pro.antdv.com only use.
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      // end
      isDev: process.env.NODE_ENV === 'development' || process.env.VUE_APP_PREVIEW === 'true',

      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      title: '住房保障系统',
      settings: {
        multiTab: defaultSettings.multiTab,
        // 布局类型
        layout: defaultSettings.layout, // 'sidemenu', 'topmenu'
        // CONTENT_WIDTH_TYPE
        contentWidth: defaultSettings.layout === 'sidemenu' ? CONTENT_WIDTH_TYPE.Fluid : defaultSettings.contentWidth,
        // 主题 'dark' | 'light'
        theme: defaultSettings.navTheme,
        // 主色调
        primaryColor: defaultSettings.primaryColor,
        fixedHeader: defaultSettings.fixedHeader,
        fixSiderbar: defaultSettings.fixSiderbar,
        colorWeak: defaultSettings.colorWeak,

        hideHintAlert: false,
        hideCopyButton: false
      },
      // 媒体查询
      query: {},

      // 是否手机模式
      isMobile: false
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: (state) => state.permission.addRouters
    }),
    ...mapGetters({
      navTheme: 'theme'
    }),
    breadcrumbRoutes () {
      const routes = this.$route.matched.concat().map(function (route) {
        return {
          path: route.path,
          breadcrumbName: route.meta.title
        }
      })
      return routes
    }
  },
  created () {
    const routes = this.mainMenu.find((item) => item.path === '/')
    this.menus = (routes && routes.children) || []
    /*
    用于当子菜单全为不显示时，菜单渲染为单个菜单
    使用场景：此菜单为一个菜单，无下级菜单，但由于使用了缓存，所有页面必须嵌套RouterView
    父级为routerView 重定向为子菜单路径
    子菜单为一个 且 meta.show = false

    ***此方法为临时解决***
    示例
    {
      name: 'test',
      parentId: 0,
      id: 1,
      meta: {
        title: '测试'
      },
      component: 'RouteView',
      redirect: '/test/page'
    },
    {
      name: 'districtChangeDeclaretestpage',
      parentId: 1,
      id: 11,
      meta: {
        title: '测试',
        show: false
      },
      path: '/test/page',
      component: 'XXX'
    }
    代码：
     this.menus.forEach((element) => {
      if (!element.children.some((item) => !item.hidden)) {
        delete element.children
      }
      })

    解决方案2：
    将真实的菜单路由隐藏 添加一个跳转菜单 重定向到地址
    {
      name: 'districtChangeDeclaretest',
      parentId: 0,
      id: 100541242,
      meta: {
        title: '测试单个'
      },
      redirect: '/test/page'
    },
    {
      name: 'testReal',
      parentId: 0,
      id: 10054124,
      meta: {
        show: false
      },
      component: 'RouteView',
      redirect: '/test/page'
    },
    {
      name: 'testPageReal',
      parentId: 10054124,
      id: 100541241,
      meta: {
        title: '测试页',
        show: false
      },
      path: '/test/page',
      component: 'XXX'
    },
    */

    // 处理侧栏收起状态
    this.$watch('collapsed', () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    })
    this.$watch('isMobile', () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    })
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }

    // first update color
    // TIPS: THEME COLOR HANDLER!! PLEASE CHECK THAT!!
    // if (process.env.NODE_ENV !== 'production' || process.env.VUE_APP_PREVIEW === 'true') {
    //   updateTheme(this.settings.primaryColor)
    // }
  },
  methods: {
    // i18nRender,
    handleMediaQuery (val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.collapsed = false
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
        // this.settings.fixSiderbar = false
      }
    },
    handleCollapse (val) {
      this.collapsed = val
    },
    handleSettingChange ({ type, value }) {
      console.log('type', type, value)
      type && (this.settings[type] = value)
      switch (type) {
        case 'contentWidth':
          this.settings[type] = value
          break
        case 'layout':
          if (value === 'sidemenu') {
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
          } else {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed
          }
          break
      }
    }
  }
}
</script>

<style lang="less">
@import './BasicLayout.less';
@keyframes fadeInUp {
  0% {
    opacity: 0;
    -webkit-transform: translate3d(0, 40px, 0);
    transform: translate3d(0, 40px, 0);
  }

  to {
    opacity: 1;
    -webkit-transform: none;
    transform: none;
  }
}
.page-fade-in {
  animation-duration: 400ms;
  animation-fill-mode: both;
  animation-name: fadeInUp;
}
</style>
